package com.example.mrzhou.lvmm.view.fragment.home;


import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.example.mrzhou.lvmm.R;
import com.example.mrzhou.lvmm.commom.base.BaseFragment;
import com.example.mrzhou.lvmm.commom.utils.FragmentViewPagerUtils;
import com.example.mrzhou.lvmm.commom.utils.ImageLoader;
import com.example.mrzhou.lvmm.commom.view.BanViewPager;
import com.example.mrzhou.lvmm.entity.home.HomeBanner;
import com.example.mrzhou.lvmm.entity.home.HomeTitle;
import com.example.mrzhou.lvmm.presenter.home.HomeBannerPresenter;
import com.example.mrzhou.lvmm.presenter.home.HomeTitlePresenter;
import com.example.mrzhou.lvmm.view.impl.home.HomeBannerView;
import com.example.mrzhou.lvmm.view.impl.home.HomeTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements HomeTitleView {

    @BindView(R.id.cb_home)
    ConvenientBanner cbHome;
    @BindView(R.id.tab_home_first)
    TabLayout tabHomeFirst;
    @BindView(R.id.bvp_home_frist)
    BanViewPager bvpHomeFrist;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private HomeTitle homeTitle;
    private List<HomeBanner.DatasBean.InfosBean> banners = new ArrayList<>();

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this,view);
        HomeTitlePresenter presenter = new HomeTitlePresenter(this);
        presenter.getHomeTitle();
        homeBanners();
    }

    /**
     * 头部广告栏
     */
    private void homeBanners() {
        cbHome.setPages(new CBViewHolderCreator<BannerViewHolder>() {

            @Override
            public BannerViewHolder createHolder() {
                return new BannerViewHolder();
            }
        },banners).setManualPageable(true);

        HomeBannerPresenter presenter = new HomeBannerPresenter(new HomeBannerView() {
            @Override
            public void onSuccess(HomeBanner homeBanner) {
                banners.clear();
                if (homeBanner!=null){
                    banners.addAll(homeBanner.getDatas().get(0).getInfos());
                    cbHome.notifyDataSetChanged();
                }
            }

            @Override
            public void onFaiure(Throwable a) {

            }

            @Override
            public void onFinsh() {

            }
        });
        presenter.getHomeBanner();
    }

    public class BannerViewHolder implements Holder<HomeBanner.DatasBean.InfosBean>{

        private ImageView iv;

        @Override
        public View createView(Context context) {
            iv = new ImageView(context);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            return iv;
        }

        @Override
        public void UpdateUI(Context context, int position, HomeBanner.DatasBean.InfosBean data) {
            ImageLoader.getInstance().displayImage(data.getLargeImage(),context,iv,R.drawable.hot_loading,R.drawable.hot_loading);
        }
    }

    /**
     * vp数据加载
     */
    @Override
    protected void initData() {
        for (int i = 0; i < titles.size(); i++) {
            fragments.add(HomeFirstFragment.newInstance(homeTitle,i));
        }
        FragmentViewPagerUtils utils = new FragmentViewPagerUtils();
        utils.fragmentVpUtils(fragments,titles,bvpHomeFrist,tabHomeFirst,getChildFragmentManager());
    }

    @Override
    public void onSuccess(HomeTitle homeTitle) {
        titles.clear();
        fragments.clear();
        if (homeTitle!=null){
            this.homeTitle = homeTitle;
            List<HomeTitle.DatasBean> datasBeen = homeTitle.getDatas();
            for (int i = 0; i < datasBeen.size(); i++) {
                titles.add(datasBeen.get(i).getTagName().substring(0,datasBeen.get(i).getTagName().length()-4));
            }
            initData();
        }
    }

    @Override
    public void onFaiure(Throwable a) {

    }

    @Override
    public void onFinsh() {

    }

    @Override
    public void onResume() {
        super.onResume();
        cbHome.startTurning(3000);
    }

    @Override
    public void onStop() {
        super.onStop();
        cbHome.stopTurning();
    }
}
